package com.turevskiy.android.tictactoe;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
	public final static String EXTRA_MESSAGE = "com.turevskiy.android.whereiam.MESSAGE";
	public ArrayList<SingleCellView> cellViews = new ArrayList<SingleCellView>();
	private int cells[] = new int[9];
	private int nextTurn = 1;
	private int xWins = 0;
	private int oWins = 0;
	private int draws = 0;

	@Override
	protected void onStart() {
		super.onStart();

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public int getCell(int i) {
		return cells[i];
	}

	public void setCell(int i, int v) {
		cells[i] = v;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_restart:
			reset();
			return true;
		case R.id.menu_about:
			AlertDialog.Builder builder = new AlertDialog.Builder(
					MainActivity.this);
			builder.setMessage(R.string.about_message)
					.setCancelable(false)
					.setPositiveButton("ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
			return true;
		case R.id.menu_stats:
			String message = getString(R.string.menu_stats) + "\nX:" + xWins
					+ ", O:" + oWins + ", -:" + draws;
			AlertDialog.Builder builder2 = new AlertDialog.Builder(
					MainActivity.this);
			builder2.setMessage(message)
					.setCancelable(false)
					.setPositiveButton("ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert2 = builder2.create();
			alert2.show();
			return true;
		case R.id.menu_exit:
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void refresh() {
		for (SingleCellView view : cellViews) {
			view.invalidate();
		}
	}

	public int isWinningPosition() {
		if ((cells[4] != 0 && cells[0] == cells[4] && cells[4] == cells[8])
				|| (cells[4] != 0 && cells[2] == cells[4] && cells[4] == cells[6])
				|| (cells[4] != 0 && cells[1] == cells[4] && cells[4] == cells[7])
				|| (cells[4] != 0 && cells[3] == cells[4] && cells[4] == cells[5]))
			return cells[4];
		else if ((cells[0] != 0 && cells[0] == cells[1] && cells[1] == cells[2])
				|| (cells[0] != 0 && cells[0] == cells[3] && cells[3] == cells[6]))
			return cells[0];
		else if ((cells[8] != 0 && cells[2] == cells[5] && cells[5] == cells[8])
				|| (cells[8] != 0 && cells[6] == cells[7] && cells[7] == cells[8]))
			return cells[8];
		else if (cells[0] != 0 && cells[1] != 0 && cells[2] != 0
				&& cells[3] != 0 && cells[4] != 0 && cells[5] != 0
				&& cells[6] != 0 && cells[7] != 0 && cells[8] != 0)
			return 0;
		else
			return -1;
	}

	public int getNextTurn() {
		int result = nextTurn;
		if (nextTurn == 1)
			nextTurn = 2;
		else
			nextTurn = 1;
		return result;
	}

	public void reset() {
		nextTurn = 1;
		for (int i = 0; i < 9; i++) {
			cells[i] = 0;
		}
		refresh();
	}

	public void checkWinner() {
		int b = isWinningPosition();

		if (b >= 0) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			String message;
			if (b == 1) {
				xWins++;
				message = getString(R.string.end_of_game)
						+ getString(R.string.x_wins);
			} else if (b == 2) {
				oWins++;
				message = getString(R.string.end_of_game)
						+ getString(R.string.o_wins);
			} else {
				draws++;
				message = getString(R.string.end_of_game)
						+ getString(R.string.draw);
			}

			builder.setMessage(message)
					.setCancelable(false)
					.setPositiveButton("ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
									reset();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
		}

	}
}
